package com.qiudaozhang.jblog.backend.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.qiudaozhang.jblog.article.service.CategoryService;
import com.qiudaozhang.jblog.common.util.CommonCode;
import com.qiudaozhang.jblog.model.Category;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 邱道长
 * 时间： 2018/12/24 11:27
 * 分类控制器
 */
@Controller
@RequestMapping("admin/category")
public class CategoryController {

    @Reference(url = "${article.service.provider.url}", version = "${service.version}", group = "${dubbo.group}")
    private CategoryService categoryService;

    @RequestMapping("index")
    public String index(){

        return "admin/category/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam("page") int pageNum, @RequestParam("limit") int pageSize,
                                   @RequestParam(required = false,name = "name") String name){
        Map<String,Object> map = new HashMap<>();
        PageInfo<Category> pageInfo =  categoryService.queryPageParam(pageNum,pageSize,name);
//        pageInfo = tagService.queryPage(pageNum,pageSize);
        map.put("code",0);
        map.put("msg","");
        map.put("data",pageInfo.getList());
        map.put("count",pageInfo.getTotal());
        return map;
    }


    @RequestMapping("delete/{categoryId}")
    @ResponseBody
    public String delete(@PathVariable("categoryId") Integer categoryId){
        categoryService.deleteById(categoryId);
        return CommonCode.SUCCESS;
    }

    @RequestMapping("post")
    @ResponseBody
    public String post(@RequestBody Category category){
        categoryService.save(category);
        return CommonCode.SUCCESS;
    }


    @RequestMapping("update")
    @ResponseBody
    public String update(@RequestBody Category category){
        categoryService.update(category);
        return CommonCode.SUCCESS;
    }


}
